<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>评论列表</title>
  <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
  <link rel="stylesheet" href="./css/reset.css">
  <link rel="stylesheet" href="./css/iconfont.css">
  <link rel="stylesheet" href="./css/main.css">
  <link rel="stylesheet" href="./libs/layui/css/layui.css">
  <script src="./libs/jquery/jquery.min.js"></script>
  <script src="./libs/layui/layui.js"></script>
</head>

<body>
  <div class="container-fluid">
    <div class="common_title">
      评论列表
    </div>
    <div class="container-fluid common_con">
      <table class="table table-striped table-bordered table-hover mp20">
        <thead>
          <tr>
            <th>作者</th>
            <th>评论</th>
            <th>评论在</th>
            <th>提交于</th>
            <th>状态</th>
            <th class="text-center" width="100">操作</th>
          </tr>
        </thead>
        <tbody id="tbody">
          <tr>
            <td>小周</td>
            <td>这是一条测试评论，欢迎光临</td>
            <td>《世界，你好》</td>
            <td>2017-07-04 12:00:00</td>
            <td>已批准</td>
            <td class="text-center">
              <a href="javascript:void(0);;" class="btn btn-warning btn-xs">拒绝</a>
              <a href="javascript:void(0);;" class="btn btn-danger btn-xs">删除</a>
            </td>
          </tr>
          <tr class="danger">
            <td>中周</td>
            <td>How are you?</td>
            <td>《世界，你好》</td>
            <td>2017-07-09 18:22:00</td>
            <td>待审核</td>
            <td class="text-center">
              <a href="javascript:void(0);;" class="btn btn-info btn-xs">批准</a>
              <a href="javascript:void(0);;" class="btn btn-danger btn-xs">删除</a>
            </td>
          </tr>

          <tr>
            <td>武秀英</td>
            <td>外影广条同取水权科速工与。矿身面却属次养导务作者用品油调。高石期品极放存斗一号口消别共去。</td>
            <td>《世界，你好》</td>
            <td>1970-03-15 11:31:50</td>
            <td>已拒绝</td>
            <td class="text-center">
              <a href="javascript:void(0);;" class="btn btn-danger btn-xs">删除</a>
            </td>
          </tr>

        </tbody>
      </table>
      <div class="row text-center" id="test1">

        <ul id="pagination" class="pagination-sm"></ul>
      </div>

    </div>
  </div>
  <!-- 引入模板引擎 -->
  <script src="./libs/art-template/template-web.js"></script>
  <!-- 定义模板 -->
  <script id="art" type="text/html">
    {{each data}}
    {{if $value.state == '已拒绝'}}
    <tr class="danger">
      {{else}}
    <tr>
      {{/if}}

      <td>{{$value.author}}</td>
      <td>{{$value.content}}</td>
      <td>{{$value.title}}</td>
      <td>{{$value.date}}&nbsp;&nbsp;{{$value.time}}</td>
      <td>{{$value.state}}</td>
      <td class="text-center">
        {{if $value.state =='已通过'}}
        <a href="javascript:void(0);;" class="btn btn-warning btn-xs" onclick="reject({{$value.id}})">拒绝</a>
        {{ else if $value.state =='已拒绝'}}
        <a href="javascript:void(0);;" class="btn btn-info btn-xs" onclick="pass({{$value.id}})">批准</a>
        {{else}}
        <a href="javascript:void(0);;" class="btn btn-warning btn-xs" onclick="reject({{$value.id}})">拒绝</a>
        <a href="javascript:void(0);;" class="btn btn-info btn-xs" onclick="pass({{$value.id}})">批准</a>
        {{/if}}

        <a href="javascript:void(0);;" class="btn btn-danger btn-xs" onclick="del({{$value.id}})">删除</a>
      </td>
    </tr>
    {{/each}}
  </script>
  <!-- 引入http.js文件 -->
  <script src="./js/http.js"></script>
  <script>
    $(function () {
      // 获取评论接口数据
      getlist();



    });


    // 获取评论接口数据
    function getlist(page, perpage) {
      let param = {
        page: page,
        perpage: perpage,
      };
      myAjax('get', '/admin/comment/search', param, function (resData) {
        // console.log(resData.data);


        // 分页控件
        paging(resData.data.totalCount, page);
        // 生成列表内容
        let resHtml = template('art', resData.data);
        $('#tbody').html(resHtml);
      });
    }


    let current = 1;
    //分页
    function paging(totalCount, page) {
      // 传出当前页数
      current = page;
      layui.use('laypage', function () {
        var laypage = layui.laypage;

        //执行一个laypage实例
        laypage.render({
          elem: 'test1' //注意，这里的 test1 是 ID，不用加 # 号
            ,
          count: totalCount //数据总数，从服务端得到
            ,
          curr: page, //得到当前页，
          limit: 6 //设定每页显示几条
            ,
          jump: function (obj, first) {
            //obj包含了当前分页的所有参数，比如：
            // console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
            // console.log(obj.limit); //得到每页显示的条数

            //首次不执行
            if (!first) {
              //do something

              // 获取评论接口数据
              getlist(obj.curr, obj.limit);
            }
          }
        });
      });
    }

    // 删除
    function del(a_id) {
      myAjax('post', '/admin/comment/delete', {
        id: a_id
      }, function (resData) {
        if (resData.code !== 200) {
          alert(resData.msg);
          return;
        }
        // 获取评论接口数据
        getlist(current);

      })
    }
    // 拒绝
    function reject(a_id) {
      myAjax('post', '/admin/comment/reject', {
        id: a_id
      }, function (resData) {
        if (resData.code !== 200) {
          alert(resData.msg);
          return;
        }
        // 获取评论接口数据
        getlist(current);

      })

    }
    // 通过
    function pass(a_id) {
      myAjax('post', '/admin/comment/pass', {
        id: a_id
      }, function (resData) {
        if (resData.code !== 200) {
          alert(resData.msg);
          return;
        }
        // 获取评论接口数据
        getlist(current);

      })

    }
  </script>


</body>

</html>